#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@gmail.com>
# CreateTime: 2018-01-19 22:35:48

from argparse import ArgumentParse

def main():
    from argparse import ArgumentParser
    parser = ArgumentParser(prog="tronitor", description="查询并监控火车票信息",
                            epilog="运行 'tronitor ACTION --help' 查看相应动作的使用帮助.")

    subparsers = parser.add_subparsers(title='actions',
                                    description="选择希望执行的操作",
                                    help="查询, 监控, 开始售票时间, 列车时刻表")

    query_parser = subparsers.add_parser("query", help="查询火车余票",
                                    description="查询火车对应坐席的余票, 默认查询所有坐席",
                                    epilog="Example: tronitor query 北京 上海 0912")
    query_parser.add_argument("from", help="出发地")
    query_parser.add_argument("to", help="目的地")
    query_parser.add_argument("date", help="出发日期")
    query_parser.add_argument("--train-types", help="指定查询的车次类型, 所有车次类型: D, G, K, T, Z")
    query_parser.add_argument("--seat-types", help="指定查询的坐席类型, 所有坐席类型: gr(高级软卧), \
        rw(软卧), rz(软座), tz(特等座), wz(无座), yw(硬卧), yz(硬座), ze(二等座), zy(一等座), sw(商务座)")

    watch_parser = subparsers.add_parser("watch", help="监控车次余票",
                                    description="监控车次对应坐席的余票信息, 当有余票时邮件通知",
                                    epilog="Example: tronitor watch --trains=D2001,Z49 --seat-types=yw,yz")
    watch_parser.add_argument("trains", help="需要监控的车次")
    watch_parser.add_argument("date", help="车票出行日期")
    watch_parser.add_argument("--seat-types", help="需要监控的坐席")
    watch_parser.add_argument("--frequency", help="查询的请求频率")
    watch_parser.add_argument("--to-mails", help="接收通知邮件的邮箱列表")

    opentime_parser = subparsers.add_parser("opentime", help="查询车站开放售票的时间",
                                        description="查询指定车站开放售票的时间",
                                        epilog="Example: tronitor opetime 郑州")
    opentime_parser.add_argument("station", help="要查询的车站")

    timetable_parser = subparsers.add_parser("timetable", help="查询列车时刻表",
                                        description="查询指定车次的运行时刻表",
                                        epilog="Example: tronitor timetable D2001")
    timetable_parser.add_argument("train", help="要查询的车次")

    options = parser.parse_args()

    str2unicode = lambda s: s.decode(sys.stdin.encoding) if sys.version_info[0] < 3 else s

    print(vars(options))

    if hasattr(options, "from"):
        try:
            date = str_to_date(options.date)
        except Exception:
            print(_c("日期格式不正确！", "red"))
            sys.exit(0)
        if date < datetime.date.today():
            print(_c("无效的查询日期！", "red"))
            sys.exit(0)

        from_station = str2unicode(getattr(options, "from"))
        to_station = str2unicode(getattr(options, "to"))
        train_types = [x.strip() for x in options.train_types.split(",")] if options.train_types else None
        seat_types = [x.strip() for x in options.seat_types.split(",")] if options.seat_types else None
        query_remaining_tickets(from_station, to_station, date, train_types, seat_types)
    elif hasattr(options, "trains"):
        pass
    elif hasattr(options, "station"):
        query_station_open_time(str2unicode(options.station))
    elif hasattr(options, "train"):
        query_train_timetable(options.train)
    else:
        parser.print_help()
